/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.dubbo.admin.registry.common.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.admin.registry.common.domain.LoadBalancePO;
import com.alibaba.dubbo.admin.registry.common.domain.OverridePO;
import com.alibaba.dubbo.admin.registry.common.domain.WeightPO;

/**
 * OverrideUtils.java
 *
 */
public class OverrideUtils {
    public static List<WeightPO> overridesToWeights(List<OverridePO> overridePOS) {
        List<WeightPO> weightPOS = new ArrayList<WeightPO>();
        if (overridePOS == null) {
            return weightPOS;
        }
        for (OverridePO o : overridePOS) {
            if (StringUtils.isEmpty(o.getParams())) {
                continue;
            } else {
                Map<String, String> params = StringUtils.parseQueryString(o.getParams());
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    if ("weight".equals(entry.getKey())) {
                        WeightPO weightPO = new WeightPO();
                        weightPO.setAddress(o.getAddress());
                        weightPO.setId(o.getId());
                        weightPO.setService(o.getService());
                        weightPO.setWeight(Integer.valueOf(entry.getValue()));
                        weightPOS.add(weightPO);
                    }
                }
            }
        }
        return weightPOS;
    }

    public static WeightPO overrideToWeight(OverridePO overridePO) {
        return overridesToWeights(Arrays.asList(overridePO)).get(0);
    }

    public static OverridePO weightToOverride(WeightPO weightPO) {
        OverridePO overridePO = new OverridePO();
        overridePO.setId(weightPO.getId());
        overridePO.setAddress(weightPO.getAddress());
        overridePO.setEnabled(true);
        overridePO.setParams("weight=" + weightPO.getWeight());
        overridePO.setService(weightPO.getService());
        return overridePO;
    }

    public static List<LoadBalancePO> overridesToLoadBalances(List<OverridePO> overridePOS) {
        List<LoadBalancePO> loadBalancePOS = new ArrayList<LoadBalancePO>();
        if (overridePOS == null) {
            return loadBalancePOS;
        }
        for (OverridePO o : overridePOS) {
            if (StringUtils.isEmpty(o.getParams())) {
                continue;
            } else {
                Map<String, String> params = StringUtils.parseQueryString(o.getParams());
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    if (entry.getKey().endsWith("loadbalance")) {
                        LoadBalancePO loadBalancePO = new LoadBalancePO();
                        String method = null;
                        if (entry.getKey().endsWith(".loadbalance")) {
                            method = entry.getKey().split(".loadbalance")[0];
                        } else {
                            method = "*";
                        }

                        loadBalancePO.setMethod(method);
                        loadBalancePO.setId(o.getId());
                        loadBalancePO.setService(o.getService());
                        loadBalancePO.setStrategy(entry.getValue());
                        loadBalancePOS.add(loadBalancePO);

                    }
                }
            }
        }
        return loadBalancePOS;
    }

    public static LoadBalancePO overrideToLoadBalance(OverridePO overridePO) {
        return OverrideUtils.overridesToLoadBalances(Arrays.asList(overridePO)).get(0);
    }

    public static OverridePO loadBalanceToOverride(LoadBalancePO loadBalancePO) {
        OverridePO overridePO = new OverridePO();
        overridePO.setId(loadBalancePO.getId());
        overridePO.setService(loadBalancePO.getService());
        overridePO.setEnabled(true);
        String method = loadBalancePO.getMethod();
        String strategy = loadBalancePO.getStrategy();
        if (StringUtils.isEmpty(method) || "*".equals(method)) {
            overridePO.setParams("loadbalance=" + strategy);
        } else {
            overridePO.setParams(method + ".loadbalance=" + strategy);
        }
        return overridePO;
    }

}
